using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BlogSystem.Ability.Docking.ReplyApp;
using BlogSystem.Ability.Docking.ReplyApp.Dto;
using BlogSystem.Domain.NewsInfo;
using Microsoft.EntityFrameworkCore;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace BlogSystem.Ability.ReplyApp
{
    public class ReplyServicer : AbilityServicer, IReplyServicer
    {   
        public IRepository<Reply> ReplyDto { get; set; }
        public async Task<bool> AddReplyDto(ReplyCreateDto createDto)
        {   
            try
            {
                var reply =  ModelMapper.Map<ReplyCreateDto, Reply>(createDto);
                reply.InitReply();
                await ReplyDto.InsertAsync(reply);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public async Task<List<ReplyDto>> GetReplyAsync(int newsId)
        {
           var replys = await ReplyDto.Where(m => m.NewsId == newsId).OrderByDescending(m => m.ReplyDate).ToListAsync();
           var dtos = ModelMapper.Map<List<Reply>, List<ReplyDto>>(replys);
           return dtos;
        }
    }
}